﻿local _, ns = ...
--名字任务图标
local NameQuestTable = {}
function ns.CteatNameQuest(unitFrame)
	if not unitFrame then
		for i, namePlate in ipairs(C_NamePlate.GetNamePlates()) do
			ns.CteatNameQuest(namePlate.UnitFrame)
		end
	end
	
	if not unitFrame then return end
	if not unitFrame.nametext then return end

	if not unitFrame.NameQuest then
		unitFrame.NameQuest = unitFrame:CreateTexture(nil, "BACKGROUND")
		unitFrame.NameQuest:SetPoint("BOTTOMLEFT",unitFrame.nametext,"BOTTOMRIGHT",-3,1)
		unitFrame.NameQuest:SetTexture("Interface\\Addons\\PlateColor\\texture\\questicon")
		unitFrame.NameQuest:Hide()
	end
	
	if not unitFrame.unit then return end
	if not unitFrame.NameQuest then return end
	
	unitFrame.NameQuest:Hide()
	
	local tooltipData = C_TooltipInfo.GetUnit(unitFrame.unit)
	if not tooltipData then return end
	for i, line in ipairs(tooltipData.lines) do
		--先判断是不是在我们的任务日志里
		if NameQuestTable[line.leftText] then
			--如果在队伍里会多一行guid
			if tooltipData.lines[i+1] and tooltipData.lines[i+1].guid and tooltipData.lines[i+1].guid == UnitGUID("player") then
				i=i+1
			end
			--再判断下一行文本是否是完成的
			if tooltipData.lines[i+1] and tooltipData.lines[i+1].completed == false then
				unitFrame.NameQuest:SetSize(unitFrame.nametext:GetHeight()*1.3,unitFrame.nametext:GetHeight()*1.3)
				unitFrame.NameQuest:Show()
				break
			end
		end
	end
end

local MyNameEvent = CreateFrame("Frame")
MyNameEvent:RegisterEvent("PLAYER_ENTERING_WORLD")
MyNameEvent:RegisterEvent("NAME_PLATE_UNIT_ADDED")
MyNameEvent:RegisterEvent("QUEST_WATCH_UPDATE")
MyNameEvent:RegisterEvent("QUEST_ACCEPTED")
MyNameEvent:RegisterEvent("QUEST_REMOVED")
MyNameEvent:SetScript("OnEvent", function(self, event, ...)
	if not PlateColorDB.nameQuest then return end
	if event == "NAME_PLATE_UNIT_ADDED" then
		local frame = ...
		local namePlate = C_NamePlate.GetNamePlateForUnit(frame,false)
		if not namePlate then return end
		local unitFrame = namePlate.UnitFrame
		ns.CteatNameQuest(unitFrame)
	end
	--进入游戏获取任务
	if event == "PLAYER_ENTERING_WORLD" then
		for i = 1, C_QuestLog.GetNumQuestLogEntries() do
			local info = C_QuestLog.GetInfo(i); 
			if info and not info.isHeader and (info.isTask or not info.isHidden) then
				NameQuestTable[info.title] = not C_QuestLog.IsComplete(info.questID)
			end 
		end
		C_Timer.After(2,function()
			ns.CteatNameQuest()
		end)
	end
	--任务进度更新
	if event == "QUEST_WATCH_UPDATE" then
        local questID = ...
        local title = C_QuestLog.GetTitleForQuestID(questID)
		if not title then return end
		if not NameQuestTable[title] then return end
		C_Timer.After(0.5,function()
			NameQuestTable[title] = not C_QuestLog.IsComplete(questID)
			ns.CteatNameQuest()
		end)
	end
	if event == "QUEST_ACCEPTED" then
        local questID = ...
		if not C_QuestLog.GetLogIndexForQuestID(questID) then return end
		local info = C_QuestLog.GetInfo(C_QuestLog.GetLogIndexForQuestID(questID))
		if info and not info.isHeader and (info.isTask or not info.isHidden) then
			NameQuestTable[info.title] = not C_QuestLog.IsComplete(questID)
			C_Timer.After(0.3,function()
				ns.CteatNameQuest()
			end)
		end
		
	end
	if event == "QUEST_REMOVED" then
        local questID = ...
        local title = C_QuestLog.GetTitleForQuestID(questID)
		if not title then return end
		NameQuestTable[title] = nil
		C_Timer.After(0.3,function()
			ns.CteatNameQuest()
		end)
	end
end)